<?php
/**
 * 应用程序基础类
 *
 * @author Mo yi <root@imoi.cn>
 * @link   http://www.imoi.cn
 * @copyright Copyright &copy; 2010-2012 DuoLam Software LLC
 * @license http://www.imoi.cn/license/
 * @version $Id: dpApplication.php 2799 2012-03-30 09:31:13Z Mo.yi $
 * @package base
 * @since 1.0
 */

class dpApplication extends dpComponent
{

	/**
	 * 应用程序构造器，初始化一些些功能
	 */
    public function __construct($config = null)
    {
        if (isset($config['import'])) {
            $this->_initLoad($config['import']);
            unset($config['import']);
        }

		if ($config !== null) {
			$this->setConfig($config);
		}

        $this->system['session_start'] && @session_start();

        define('ROOT', D::homeUrl());

		D::setPathAlias('public',APP_PATH . '/public/');
		D::setPathAlias('runtime',APP_PATH . '/runtime/');
		D::setPathAlias('app',APP_PATH . '/app/');
        D::setPathAlias('widget',APP_PATH . '/app/widget/');
        D::setPathAlias('system',DP_PATH . DS);
        D::setApp($this);

        $this->initHandlers();
        $this->setGzip();
        $this->setCharset();
        $this->setTimezone();
        $this->registerCoreComponent();
        D::app()->url();
    }

    /**
     * 程序异常报错函数
     */
    public function exceptionHandler(Exception $e)
    {
        dpException::error($e->getMessage());
    }

    /**
     * 程序错误报错函数
     */
    public function errorHandler($ecode,$emsg,$efile,$eline)
    {
         dpException::error($emsg);
    }

    /**
     * 设置捕获异常和错误
     */
    protected function initHandlers()
    {
        if ($this->system['debug_model']) {
            set_exception_handler(array($this,'exceptionHandler'));
            set_error_handler(array($this,'errorHandler'),error_reporting($this->system['trace_level']));
		} else {
			error_reporting($this->system['trace_level']);
        }
    }

    /**
     * 初始化应用程序自定义加载目录
     * @param $import array
     */
    protected function _initLoad($import)
    {
		D::$classMap = $import;
    }

    /**
     * 设置应用程序的时区
     */
    public function setTimezone()
    {
        PHP_VERSION > '5.1' && @date_default_timezone_set($this->system['timezone']);
    }

    /**
     * 设置页面Gzip压缩
     */
    public function setGzip()
    {
        if ($this->system['gzip'] && function_exists('gz_gzhandler'))
            ob_start('gz_gzhandler');
    }

    /**
     * 设置应用程序的字符集
     * @return string
     */
    public function setCharset()
    {
        header('Content-type: text/html;charset='.$this->system['charset']);
    }

    /**
     * 注册核心组件
     */
    public static function registerCoreComponent()
    {
        return array(
            'getDb'       => array(
                'class'   => 'dpDbConnect'
            ),
            'url'         => array(
                'class'   => 'dpUrlConfig'
            ),
            'fileCache'   => array(
                'class'   => 'dpFileCache'
            ),
            'apcCache'    => array(
                'class'   => 'dpApcCache'
            ),
            'memCache'    => array(
                'class'   => 'dpMemCache'
            ),
            'XCache'      => array(
                'class'   => 'dpXCache'
            ),
            'mysqlSchema' => array(
                'class'   => 'dpMysqlSchema'
            ),
            'sqliteSchema'=> array(
                'class'   => 'dpSqliteSchema'
            ),
            'mssqlSchema' => array(
                'class'   => 'dpMssqlSchema'
            ),
			'script'	  => array(
				'class'   => 'dpScript'
			),
            'user'        => array(
                'class'   => 'dpUser'
            ),
            'auth'        => array(
                'class'   => 'dpAuth'
            ),
            'rule'       => array(
                'class'   => 'dpAuthRule'
            ),
            'filter'      => array(
                'class'   => 'dpAuthFilter'
            )
        );
    }

}

?>